import { Store } from "../../core";

import GoToPreviousPageButton from "./GoToPreviousPageButton";
import StoreProps from "./StoreProps";
import { RenderGoToPage, RenderGoToPageProps } from "./types/index";
import useCurrentPage from "./useCurrentPage";
import { FC } from "react";

const GoToPreviousPage: FC<{
  children?: RenderGoToPage;
  store: Store<StoreProps>;
}> = ({ store, children }) => {
  const { currentPage } = useCurrentPage(store);

  const goToPreviousPage = () => {
    const jumpToPage = store.get("jumpToPage");
    if (jumpToPage) {
      jumpToPage(currentPage - 1);
    }
  };

  const defaultChildren = (props: RenderGoToPageProps) => (
    <GoToPreviousPageButton
      isDisabled={props.isDisabled}
      onClick={props.onClick}
    />
  );
  const render = children || defaultChildren;

  return render({
    isDisabled: currentPage <= 0,
    onClick: goToPreviousPage,
  });
};

export default GoToPreviousPage;
